/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.p10.miage.gestionnote.action;

import fr.p10.miage.gestionnote.model.EcIntervenant;
import fr.p10.miage.gestionnote.model.EcsIntervenant;
import fr.p10.miage.gestionnote.modelservice.EcsIntervenantServices;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionRedirect;

/**
 *
 * @author charleshenzel
 */
public class ECPublierEcAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "publier-ec-success";
    private static final String ERROR = "publier-ec-error";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
         
        int idec = Integer.parseInt(request.getParameter("idec"));
        int idf = Integer.parseInt(request.getParameter("idf"));
        int idp = Integer.parseInt(request.getParameter("idp"));
        
        ActionMessages errors = new ActionMessages();    

        EcsIntervenant listeEcsIntervenant = (EcsIntervenant) request.getSession().getAttribute("listeEcsIntervenant");
        
        EcIntervenant ecIntervenant = listeEcsIntervenant.getEcIntervenantById(idec,idp);
        
        int nbControles = ecIntervenant.getControles().size();
        
        if(nbControles != EcsIntervenantServices.calculNbControlesPublier(idec,idp))
        {
             errors.add("publier", new ActionMessage("errors.publier.ec", "controle"));
             saveErrors(request.getSession(), errors);
             ActionRedirect redirect = new ActionRedirect(mapping.findForward(ERROR));
             redirect.addParameter("idec", idec);
             redirect.addParameter("idp", idp);
             return redirect;
        }
        else
        {
            EcsIntervenantServices.publierEc(idec,idf,idp);
        }
        
//        System.out.println("publier : "+ecIntervenant.isPublier());
        
        ActionMessages messages = new ActionMessages();
        messages.add("succesInsertPublierEc", new ActionMessage("succes.publier.ec",ecIntervenant.getNom()));
        saveMessages(request.getSession(), messages);
        
        ActionRedirect redirect = new ActionRedirect(mapping.findForward(SUCCESS));
        redirect.addParameter("idec", idec);
        redirect.addParameter("idp", idp);
        return redirect;
    }
}
